package com.xunyu.shopping_manager_api.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.secure.SaSecureUtil;
import cn.dev33.satoken.stp.StpUtil;
import co.elastic.clients.elasticsearch.xpack.usage.Base;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinyu.shopping_common.entity.Admin;
import com.xinyu.shopping_common.result.BaseResult;
import com.xinyu.shopping_common.service.IAdminService;
import com.xunyu.shopping_manager_api.service.ILoginService;
import io.micrometer.core.instrument.binder.BaseUnits;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin")
public class AdminController {

    @DubboReference
    private IAdminService adminService;

    @Autowired
    private ILoginService loginService;

    /**
     * 新增管理员
     *
     * @param admin 管理员对象
     * @return
     */
    @PostMapping("/add")
    public BaseResult add(@RequestBody Admin admin) {
        //原密码加密
        String md5 = SaSecureUtil.md5(admin.getPassword());
        admin.setPassword(md5);
        adminService.add(admin);
        return BaseResult.success();
    }

    /**
     * 修改管理员
     *
     * @param admin 管理员对象
     * @return
     */
    @PutMapping("/update")
    public BaseResult update(@RequestBody Admin admin) {
        //原密码加密
        String md5 = SaSecureUtil.md5(admin.getPassword());
        admin.setPassword(md5);
        adminService.update(admin);
        return BaseResult.success();
    }

    /**
     * 根据主键删除数据
     *
     * @param aid 主键id
     * @return
     */
    @DeleteMapping("/delete")
    public BaseResult delete(Long aid) {
        adminService.delete(aid);
        return BaseResult.success();
    }

    /**
     * 根据主键查询数据
     *
     * @param aid 主键id
     * @return
     */
    @GetMapping("/findById")
    public BaseResult findById(Long aid) {
        return BaseResult.success(adminService.findById(aid));
    }

    /**
     * 分页查询
     *
     * @param page 页码
     * @param size 每页条数
     * @return
     */
    @GetMapping("/search")
    @SaCheckPermission("/admin/search")
    public BaseResult search(@RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "10") Integer size) {
        Page<Admin> result = adminService.search(page, size);
        return BaseResult.success(result);

    }

    /**
     * 修改管理员的角色
     *
     * @param aid  管理员id
     * @param rids 角色id
     * @return
     */
    @PutMapping("/updateRoleToAdmin")
    public BaseResult updateRoleToAdmin(Long aid, Long[] rids) {
        adminService.updateRoleToAdmin(aid, rids);
        return BaseResult.success();
    }

    /**
     * 登录功能
     *
     * @param username 账号
     * @param password 密码
     * @return
     */
    @PostMapping("/login")
    public BaseResult login(String username, String password) {
        return loginService.login(username, password);
    }

    /**
     * 获取当前登录账号
     *
     * @return
     */
    @GetMapping("/getUsername")
    public BaseResult getUsername() {
        //获取Sa-token框架的登录id （账号）
        String username = StpUtil.getLoginIdAsString();
        return BaseResult.success(username);
    }

    /**
     * 退出登录
     * @return
     */
    @PostMapping("/logout")
    public BaseResult logout() {
        StpUtil.logout();
        return BaseResult.success();
    }


}
